package com.test2;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.events.EventFiringWebDriver;

public class YXMUtil2 {

    public static void main(String[] args) {
        String path = "C:\\Program Files\\Google\\Chrome\\Application\\chromedriver.exe";
        if(args.length > 0){
            path = args[0];
        }
        // 参数配置
        System.setProperty("webdriver.chrome.driver", path);
        ChromeOptions option = new ChromeOptions();
//        option.addArguments("headless"); // 无界面参数
//        option.addArguments("no-sandbox"); // 禁用沙盒
        // 通过ChromeOptions的setExperimentalOption方法，传下面两个参数来禁止掉谷歌受自动化控制的信息栏
        //option.setExperimentalOptions("useAutomationExtension", false);
        //option.setExperimentalOptions("excludeSwitches", Collections.singletonList("enable-automation"));
        //设置为 headless 模式 （必须）
        //option.addArguments("--headless");
        option.addArguments("--disable-gpu");//谷歌文档提到需要加上这个属性来规避bug
        option.addArguments("--no-sandbox");// “–no - sandbox”参数是让Chrome在root权限下跑
        option.addArguments("--disable-dev-shm-usage");
        option.addArguments("lang=zh_CN.UTF-8");
        option.setExperimentalOption("detach", true);
        //option.addArguments("window-size=1920x1080"); //指定浏览器分辨率
        WebDriver chromeDriver = new ChromeDriver(option);
        String title = "";
        String num = "";
        String img = "";

        // 创建EventFiringWebDriver 实例
        EventFiringWebDriver driver= new EventFiringWebDriver(chromeDriver);
        // Creating instance of eventListener, that we just defined
        MyEventListener eventListener1=new MyEventListener();
        // Register the Listener with the event firing driver
        //driver.register(eventListener1);
        driver.get("https://www.baidu.com");
        WebElement searchBox = driver.findElement(By.name("wd"));
        searchBox.click();
        searchBox.sendKeys("Hello World");
        searchBox.submit();
    }

}

